package com.atsugon.demo02.controller;

import com.atsugon.demo02.bean.Alert;
import com.atsugon.demo02.bean.AlertCount;
import com.atsugon.demo02.bean.Xmlbean;
import com.atsugon.demo02.service.AlertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AlertController {

    @Autowired
    AlertService alertService;

    /*
    * MyBatisPlus的CRUD
    * */

    //mybatisplus返回全部
    @GetMapping("/mybatis/allAlert")
    public List<Alert> getallAlert(){
        return alertService.list();
    }
    //mybatisplus查
    @GetMapping("/mybatis/find/{id}")
    public Alert getById(@PathVariable("id") Long id){
        return alertService.getById(id);
    }
    //mybatisplus增
    @PostMapping("/mybatis/add")
    public String addAlert(@RequestBody Alert alert){
        if (alertService.save(alert)){
            return "插入成功！";
        }
        else {
            return "插入失败！";
        }
    }
    //mybatisplus删
    @GetMapping("/mybatis/delete/{id}")
    public String removeById(@PathVariable("id") Long id){
        if (alertService.removeById(id)){
            return "删除成功！";
        }
        else {
            return "删除失败！";
        }
    }
    //mybatisplus改
    @PostMapping("/mybatis/update")
    public String updateById(@RequestBody Alert alert){
        if (alertService.updateById(alert)){
            return "更新成功！";
        }
        else {
            return "更新失败！";
        }
    }


    /*
     * Spring注解的CRUD
     * */
    @GetMapping("/insert")
    public String InsertById(@RequestParam(value = "rowname") String rowname,
                             @RequestParam("id") Integer id){
        return alertService.InsertById(rowname,id);
    }

    @GetMapping("/delete")
    public String DeleteById(@RequestParam("id") Integer id){

        return alertService.DeleteById(id);
    }

    @GetMapping("/update")
    public String UpdateStatus(@RequestParam("id") Integer id,
                               @RequestParam("status") Integer status){
        return alertService.UpdateStatus(id,status);
    }

    /*
     * 按具体日、周、月、年查询报警次数
     * 使用注解的方式
     * 分Get/Post实现
     * */
    @GetMapping("/day")
    public String getday(@RequestParam("date") String date){
        return "该天的报警次数:"+ alertService.DayselectByDate(date);
    }

    @GetMapping("/week")
    public String getweek(@RequestParam("date") String date){
        return "该周的报警次数:"+ alertService.WeekselectByDate(date);
    }

    @GetMapping("/month")
    public String getmonth(@RequestParam("date") String date){
        return "该月的报警次数:"+ alertService.MonthselectByDate(date);
    }

    @GetMapping("/year")
    public String getyear(@RequestParam("date") String date){
        return "该年的报警次数:"+ alertService.YearselectByDate(date);
    }

    @PostMapping("/postday")
    public String postgetday(@RequestParam("date") String date){
        return "该天的报警次数:"+ alertService.DayselectByDate(date);
    }

    @PostMapping("/postweek")
    public String postgetweek(@RequestParam("date") String date){
        return "该周的报警次数:"+ alertService.WeekselectByDate(date);
    }

    @PostMapping("/postmonth")
    public String postgetmonth(@RequestParam("date") String date){
        return "该月的报警次数:"+ alertService.MonthselectByDate(date);
    }

    @PostMapping("/postyear")
    public String postgetyear(@RequestParam("date") String date){
        return "该年的报警次数:"+ alertService.YearselectByDate(date);
    }

    /*
     * 尝试返回xml格式
     * */
    @GetMapping(path = "/xml",produces = "application/xml;charset=utf-8")
    public Xmlbean sendXml(@RequestParam("xml") String xml){
        Xmlbean xmlbean = new Xmlbean();
        xmlbean.setXmlmsg(xml);
        return xmlbean;
    }

    /*
     * 按日、周、月、年分类查询报警次数
     * 以xml方式查询
     * 分页查询
     * */
    @GetMapping("/xmlday")
    public List<AlertCount> xmlgetday(){
        return alertService.xmlDayselectByDate();
    }
    @GetMapping("/xmldaypage")
    public List<AlertCount> xmlgetdaypage(@RequestParam("pfrom") Long pfrom,
                                          @RequestParam("pto") Long pto){
        return alertService.xmlDayselectByDateLimitPage(pfrom,pto);
    }
    @GetMapping("/xmlweek")
    public List<AlertCount> xmlgetweek(){
        return alertService.xmlDayselectByWeek();
    }
    @GetMapping("/xmlweekpage")
    public List<AlertCount> xmlgetweekpage(@RequestParam("pfrom") Long pfrom,
                                           @RequestParam("pto") Long pto){
        return alertService.xmlDayselectByWeekLimitPage(pfrom,pto);
    }
    @GetMapping("/xmlmonth")
    public List<AlertCount> xmlgetmonth(){
        return alertService.xmlDayselectByMonth();
    }
    @GetMapping("/xmlmonthpage")
    public List<AlertCount> xmlgetmonthpage(@RequestParam("pfrom") Long pfrom,
                                            @RequestParam("pto") Long pto){
        return alertService.xmlDayselectByMonthLimitPage(pfrom,pto);
    }
    @GetMapping("/xmlyear")
    public List<AlertCount> xmlgetyear(){
        return alertService.xmlDayselectByYear();
    }
    @GetMapping("/xmlyearpage")
    public List<AlertCount> xmlgetyearpage(@RequestParam("pfrom") Long pfrom,
                                           @RequestParam("pto") Long pto){
        return alertService.xmlDayselectByYearLimitPage(pfrom,pto);
    }
}
